﻿using BCService;
using BCService.Common.OSS;
using BCService.Common.UploadFileRecord;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;

namespace OSSConsole
{
    class Program
    {
        public static IServiceCollection services { get; set; }
        public static IConfiguration configuration { get; set; }
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder();
#if DEBUG
            Console.WriteLine("DEBUG");
            builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
#else
            Console.WriteLine("RELEASE");
             builder.AddJsonFile("/home/web/bcapi/appsettings.production.json", optional: true, reloadOnChange: true);
#endif
            configuration = builder.Build();
            GeneralContainer.Configuration = configuration;
            services = new ServiceCollection()
                 .AddLogging()
                 .AddScoped<ILoggerFactory, LoggerFactory>()
                 .AddScoped<IApplicationContextService, ConsoleContextService>()
                 .AddServices()
                 .AddMapperConfiguration();
            services.AddSingleton(typeof(IConfiguration), configuration);
            Console.WriteLine("Service Count:" + services.Count);
            IServiceProvider serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions().ValidateScopes);

            ILoggerFactory loggerFactory = serviceProvider.GetService<ILoggerFactory>();
            //全局服务提供
            GeneralContainer.ServiceProvider = serviceProvider;

            TransportRecord(serviceProvider);
        }

        private static void TransportRecord(IServiceProvider serviceProvider)
        {
            IOSSObject ossObject = serviceProvider.GetService<IOSSObject>();
            IUploadFileRecordService uploadFileRecordService = serviceProvider.GetService<IUploadFileRecordService>();
            var list = uploadFileRecordService.GetNoneTransportList();
            string bucketName = configuration["AliyunOSSAPI:Buckets:0:name"];
            Console.WriteLine("总计:" + list.Count() + "条数据");
            int i = 0;
            int success = 0;
            int failed = 0;
            int smallFailed = 0;
            int smallSuccess = 0;
            foreach (var item in list)
            {

                string localFileName = Path.Combine(item.FilePath, item.UploadFileRecordNo + item.FileSuffix);
                string smallLocalFileName = Path.Combine(item.FilePath, item.UploadFileRecordNo + "_50x50" + item.FileSuffix);
                string objectName = item.FileUrl;
                string smallObjectName = item.FileUrl.Replace(item.UploadFileRecordNo, item.UploadFileRecordNo + "_50x50");
                if (!ossObject.DoesObjectExist(bucketName, objectName))
                {
                    if (!ossObject.PutObjectCallback(bucketName, objectName, localFileName, item.UploadFileRecordNo, out bool fileExists))
                    {
                        failed++;
                    }
                    else
                    {
                        success++;
                    }

                    if (!fileExists)
                    {
                        uploadFileRecordService.Delete(item.UploadFileRecordNo);
                    }
                    if (item.FileSuffix != ".mp4")
                    {
                        if (!ossObject.PutObjectCallback(bucketName, smallObjectName, smallLocalFileName, item.UploadFileRecordNo, out bool smallFileExists))
                        {
                            smallFailed++;
                        }
                        else
                        {
                            smallSuccess++;
                        }
                    }

                }
                i++;
                if (i % 100 == 0)
                {
                    Console.WriteLine(i);
                }
            }
            Console.WriteLine("总计: " + list.Count() + " 条数据");
            Console.WriteLine("成功: " + success + " 条数据");
            Console.WriteLine("失败: " + failed + " 条数据");
            Console.WriteLine("缩略图成功: " + smallSuccess + " 条数据");
            Console.WriteLine("缩略图失败: " + smallFailed + " 条数据");
            Console.WriteLine("同步结束!");

        }
    }
}
